/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.googlecode.duplicatedetector.Constants;
import com.googlecode.duplicatedetector.properties.PropertiesManager;

/**
 * Logger class.
 * 
 * @author Marco Biscaro
 */
public final class Logger implements Constants {

	private static final DateFormat FORMAT = new SimpleDateFormat(
			"dd/MM/yy HH:mm:ss.mmm");
	private static boolean debug = Boolean.valueOf(PropertiesManager
			.getProperty("debug"));
	private static PrintStream logStream;
	private Class<?> clazz;
	private PrintStream[] outs;

	static {
		try {
			logStream = new PrintStream(new FileOutputStream(File
					.createTempFile("duplicatedetector", ".log")), true);
		} catch (FileNotFoundException e) {
			logStream = null;
		} catch (IOException e) {
			logStream = null;
		}
	}

	public Logger(Class<?> c) {
		this(c, System.out, logStream);
	}

	public Logger(Class<?> c, PrintStream... outs) {
		clazz = c;
		this.outs = outs;
	}

	public synchronized void log(Level level, String message) {
		for (PrintStream out : outs) {
			if (out == null) {
				continue;
			}
			if (debug) {
				out.println("[" + FORMAT.format(new Date()) + "] - "
						+ clazz.getName());
			}
			out.printf("[%8s] %s%n", level, message);
		}
	}

	public void debug(String message) {
		if (!debug) {
			return;
		}
		log(Level.DEBUG, message);
	}

	public void warning(String message) {
		if (!debug) {
			return;
		}
		log(Level.WARNING, message);
	}

	public void info(String message) {
		log(Level.INFO, message);
	}

	public void error(String message) {
		log(Level.ERROR, message);
	}

	public void exception(String message, Throwable t) {
		log(Level.ERROR, message);
		for (StackTraceElement e : t.getStackTrace()) {
			error(e.toString());
		}
	}

	public static boolean isDebug() {
		return debug;
	}

	public static void setDebug(boolean debug) {
		Logger.debug = debug;
		PropertiesManager.setProperty("debug", debug);
	}

	public enum Level {
		DEBUG, INFO, WARNING, ERROR;
	}

}
